package net.lzzy.practices.web.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Option;
import net.lzzy.practices.entities.Question;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.QuestionService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author liaodcc on 2022/6/1 16:49.
 */
@Api(tags = "题目相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_QUESTION_ROOT)
public class QuestionApiController {
    private final QuestionService service;

    @Autowired
    public QuestionApiController(QuestionService service) {
        this.service = service;
    }

    @GetMapping(ApiConstants.ROUTE_QUESTION_CHAPTER)
    @ApiOperation(value = "获取某课程的某个章节的所有题目",notes = "1.token(String)<br/>2.chapterId(String)<br/>")
    public ApiResult get(@RequestParam(name = "id")UUID chapterId,@RequestParam String token){
        boolean teacher = !AuthUtils.illegalUser(token,new int[]{User.TYPE_TEACHER});
        List<VmQuestion> questions = new ArrayList<>();
        service.get(chapterId).forEach(question -> questions.add(VmQuestion.create(question,teacher)));
        return new ApiResult(true,questions.size() + "",questions);
    }
    private ApiResult create(JSONObject json){
        String token = json.getString(ApiConstants.KEY_TOKEN);
        String content = json.getString(ApiConstants.KEY_QUESTION_CONTENT);
        String analysis = json.getString(ApiConstants.KEY_QUESTION_ANALYSIS);
        Integer ordinal = json.getInteger(ApiConstants.KEY_QUESTION_ORDINAL);
        String strChapterId = json.getString(ApiConstants.KEY_QUESTION_CHAPTER_ID);
        Integer type = json.getInteger(ApiConstants.KEY_QUESTION_TYPE);
        if (StringUtils.isEmpty(token) ||
        StringUtils.isEmpty(content) ||
        StringUtils.isEmpty(analysis) ||
        StringUtils.isEmpty(strChapterId) ||
        ordinal == null || type == null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID chapterId = UUID.fromString(strChapterId);
        Question question = new Question();
        JSONArray array = json.getJSONArray(ApiConstants.KEY_QUESTION_OPTIONS);
        List<Option> options = new ArrayList<>();
        if (array != null && array.size() > 0){
            for (int i= 0 ;i < array.size();i++){
                JSONObject jo = array.getJSONObject(i);
                Option option = new Option();
                option.setAnswer(jo.getBoolean(ApiConstants.KEY_OPTION_ANSWER));
                String oc = jo.getString(ApiConstants.KEY_OPTION_CONTENT);
                String ol = jo.getString(ApiConstants.KEY_OPTION_LABEL);
                if (StringUtils.isEmpty(oc) || StringUtils.isEmpty(ol)){
                    return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
                }
                option.setContent(oc);
                option.setLabel(ol);
                option.setQuestion(question);
                options.add(option);
            }
        }
        question.setOptions(options);
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setOrdinal(ordinal);
        Question result = service.add(chapterId,question,token);
        if (result == null){
            return new ApiResult(false,Messages.WRONG_ID + "或" + Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @ApiOperation(value = "添加题目",notes = "1.token(String)<br/>2.content(String)<br/>3.analysis(String)<br/>"+
            "4.ordinal(int)<br/>5.chapterId(String)<br/>6.type(int)<br/>7.options(Array)<br/>8.&nbsp;&nbsp;[<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>"+
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.1content(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.2label(String)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.3answer(boolean)<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;]")
    @PostMapping(ApiConstants.ROUTE_QUESTION_ADD)
    public ApiResult add(@RequestBody JSONObject json){
        return create(json);
    }

    private ApiResult update(UUID id, QuestionPutInfo info){
        String token = info.getToken();
        String content = info.getContent();
        String analysis = info.getAnalysis();
        Integer ordinal = info.getOrdinal();
        Integer type = info.getType();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(content) ||
                StringUtils.isEmpty(analysis) ||
                ordinal == null || type == null || id == null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Question question = new Question();
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setType(type);
        question.setOrdinal(ordinal);
        Question result = service.update(id,question,token);
        if (result == null){
            return new ApiResult(false,Messages.WRONG_ID + "或" + Messages.NO_PERMISSION, null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }
    @PostMapping(ApiConstants.ROUTE_QUESTION_PUT)
    @ApiOperation(value = "更新题目",notes = "1.id(UUID)<br/>2.token(String)<br/>3.content(String)<br/>4.analysis(String)<br/>"+
            "5.type(int)<br/>6.ordinal(int)<br/>")
    public ApiResult put(@PathVariable UUID id,@RequestBody QuestionPutInfo info){
        return update(id,info);
    }
    @PostMapping(ApiConstants.ROUTE_QUESTION_OPTION_ADD)
    @ApiOperation(value = "添加题目的选项",notes = "1.token(String)<br/>2.strQuestionId(String)<br/>3.content(String)<br/>4.label(String)<br/>5.answer(boolean)<br/>")
    public ApiResult addOption(@RequestBody OptionInfo info){
        String token = info.getToken();
        String strQuestionId = info.getQuestionId();
        String content = info.getContent();
        String label = info.getLabel();
        Boolean answer = info.getAnswer();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(content) ||
                StringUtils.isEmpty(strQuestionId) ||
                StringUtils.isEmpty(label) || answer == null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Option option = new Option();
        option.setContent(content);
        option.setLabel(label);
        option.setAnswer(answer);
        UUID questionId = UUID.fromString(strQuestionId);
        Question question = service.addOption(questionId,option,token);
        if (question == null){
            return new ApiResult(false,Messages.WRONG_ID + "或" + Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }
    @PostMapping(ApiConstants.ROUTE_QUESTION_OPTION_DEL)
    @ApiOperation(value = "移除题目的选项",notes = "1.token(String)<br/>2.strId(String)<br/>")
     public ApiResult removeOption(@RequestBody OptionKeyInfo info){
        String token = info.getToken();
        String strId = info.getOptionId();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID id = UUID.fromString(strId);
        Question question = service.removeOption(id,token);
        if (question == null){
            return new ApiResult(false,Messages.WRONG_ID + "或"+ Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmQuestion.create(question,true));
     }

     @DeleteMapping(ApiConstants.ROUTE_QUESTION_DEL)
     @ApiOperation(value = "删除题目",notes = "1.token(String)<br/>2.strId(String)<br/>")
    public ApiResult remove(@RequestBody OptionKeyInfo info){
         String token = info.getToken();
         String strId = info.getOptionId();
         if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strId)){
             return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
         }
         UUID id = UUID.fromString(strId);
         Messages msg = service.remove(id,token);
         return new ApiResult(msg == Messages.SUCCESS,msg.toString(),null);
     }
}
